package com.woniuxy.consumer;

import com.rabbitmq.client.Channel;
import com.woniuxy.entity.Orders;
import com.woniuxy.mapper.OrdersMapper;
import com.woniuxy.util.RabbitMQConst;
import com.woniuxy.util.RabbitMQMessageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 延迟消费者：用于处理用户超时15分钟未支付取消订单的业务
 */
@Slf4j
@Component
public class DelayConsumer {
    @Resource
    private OrdersMapper ordersMapper;

    @RabbitListener(queues = RabbitMQConst.DELAY_QUEUE)
    public void receiveDelayMessage(String orderId, Channel channel, Message message) {
        RabbitMQMessageHelper.processMessageWithAck(channel, message, msg -> {
            Orders orders = ordersMapper.selectOrderByOrderId(orderId);
            if (orders.getOrderStatus() == 1) {
                // 待支付状态，取消订单
                Orders orders1 = new Orders();
                orders1.setOrderId(orderId);
                orders1.setOrderStatus(7); // 交易关闭
                ordersMapper.updateOrderStatus(orders1);
            }
        });
    }
}
